<?php

$oUrl = &App::getUrl();
$oReq = &App::getRequest();
$oTpl = &App::getTemplate();
$oDb = &Database::get();
$oLang = &App::getLanguage();

$iPage = $oReq->get('page', 1);
$iPageSize = 20;

$iId = $oReq->get('id');

if ($oReq->get('revert-selected')) 
{
	$aDelete = $oReq->getArray('deleteid');

	foreach($aDelete as $iDelete)
	{
		if ( !empty($iDelete) )
		{
			$aRow = $oDb->getRow("SELECT phraseid, `default` FROM " . App::getT('language_phrase') . " WHERE phraseid = '" . $iDelete . "'");
			$oDb->update(App::getT('language_phrase'), array('text' => $aRow['default']), "phraseid = '" . $iDelete . "'");	
		}
	}
	
	$aRequests = &$oReq->getAll(array('save', 'text', 'revert-selected'));
	$aReturn = array();
	foreach($aRequests as $n => $v)
	{
		$aReturn[$n] = $v;
	}	

	$sCacheText = App::format('Mod_Language.phrases_reverted');
	
	$oLang->removeCachedFiles();
	
	App::gotoUrl('admin.language.phrases', $aReturn, $sCacheText, false);
}

if ( $oReq->get('delete') )
{
	$aDelete = $oReq->getArray('deleteid');

	foreach($aDelete as $iDelete)
	{
		if ( !empty($iDelete) )
		{
			$oDb->_delete(App::getT('language_phrase'), "phraseid = '". $iDelete ."'");
		}
	}

	$aRequests = &$oReq->getAll(array('save', 'text', 'delete'));
	$aReturn = array();
	foreach($aRequests as $n => $v)
	{
		$aReturn[$n] = $v;
	}	
	$sCachePhrase = App::format('Mod_Language.phrases_deleted');
	$oLang->removeCachedFiles();
	
	App::gotoUrl('admin.language.phrases', $aReturn, $sCachePhrase, false);
}

if ( $oReq->get('save') )
{
	$aVal = $oReq->getArray('text');
	$aRemoveResult = array();
	foreach($aVal as $iNewId => $sNewText)
	{
		$aQuery = array
		(
			'text' => $sNewText,
		);
		$oDb->update(App::getT('language_phrase'), $aQuery, "phraseid = '". $iNewId ."'");
	}

	$aRequests = &$oReq->getAll(array('save', 'text'));
	$aReturn = array();
	foreach($aRequests as $n => $v)
	{
		$aReturn[$n] = $v;
	}
	
	$sCachePhrase = App::format('language_phrase_updated');
	$oLang->removeCachedFiles();

	if ( $sReturn = $oReq->get('lreturn') )
	{
		if ( $sReturn == 'index' )
		{
			App::gotoUrl('public');
		}
		else
		{
			App::gotoUrl('public.'. ($oReq->get('lpopup') ? 'popup' : 'content') .'', array('dir' => 'language', 'file' => $sReturn));
		}
	}
	else
	{
		App::gotoUrl('admin.language.phrases', $aReturn, $sCachePhrase, false);
	}
}

$sQ = "";

if ( $iId )
{
	$sQ .= " AND p.languageid = '". $oDb->escape($iId) ."' ";
}

if ( $sQuery = $oReq->get('q') )
{
	$sType = $oReq->get('type');

	switch($sType)
	{
		case 'varname':
		$sQ .= " AND p.`varname` LIKE '%". $oDb->escape($sQuery) ."%' ";
		break;
		case 'text':
		default:
		$sQ .= "AND p.`text` LIKE '%". $oDb->escape($sQuery) ."%'";
		break;
		case 'both':
		$sQ .= " AND (p.`varname` LIKE '%". $oDb->escape($sQuery) ."%' OR p.`text` LIKE '%". $oDb->escape($sQuery) ."%') ";
		break;
	}
}

if ( $sModule = $oReq->get('module') )
{
	$sQ .= " AND p.`module` = '". str_replace("-", "_", $oDb->escape($sModule)) ."' ";
}

if ( $sContent = $oReq->get('content') )
{
	if ( $sContentSearch = $oReq->get('csearch') )
	{
		$sQ .= " AND p.`varname` = 'site_content_". str_replace("-", "_", $sContentSearch) ."' ";
	}
	else
	{
		$sQ .= " AND p.`varname` LIKE 'site_content_%' ";
	}
}

$iCnt = $oDb->getField("SELECT COUNT(*) FROM ". App::getT('language_phrase') ." AS p
	WHERE 1 ". $sQ ."");

$iOffset = $oDb->_getPageNum($iPage, $iPageSize, $iCnt);

$aItems = $oDb->getRows("SELECT p.`phraseid`, p.`varname`, p.`text`, p.`default`, l.`title` FROM
	". App::getT('language_phrase') ." AS p
	LEFT JOIN ". App::getT('language') ." AS l ON(p.languageid = l.languageid)
	WHERE 1 ". $sQ ."
	ORDER BY `phraseid` DESC LIMIT ". $iOffset .",". $iPageSize ."");

$oPager = App::newObject('Pager', $iCnt, $iPage, $iPageSize);

$aRequests = &$oReq->getAll(array('page', 'save', 'deleteid', 'delete'));
if ( count($aRequests) > 0 )
{
	foreach($aRequests as $n => $v)
	{
		$oUrl->setParam($n, $v);
	}
}

$iFrom = $oPager->getOffset($iPage, $iPageSize);
$iTo = (($iFrom + $iPageSize) > $iCnt) ? $iCnt : ($iFrom + $iPageSize);

$aModules = App::getParam('modules');
sort($aModules);

$oTpl->assignSrc(array
(
	'iId' => $iId,
	'aItems' => $aItems,
	'aPaging'   => $oPager->getInfo($oUrl),
	'aLanguages' => $oLang->getLanguages(),
	'aModules' => $aModules,
	'aPost' => $aRequests,
	'iCnt'             => $iCnt,
	'iTo'              => $iTo,
	'iFrom'            => ($iCnt) ? ($iFrom + 1) : 0,
));

?>